// pages/mine/adress/adress.js
const app = getApp()
import Notify from '@vant/weapp/notify/notify';
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
    user:{},
    list:[{
      tag:'家',
      address:'河南省新乡市封丘县北街'
    }],
    modalName:null,
    address:'',
    tag:'',
    loadModal:true
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {

	},
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
    var user=wx.getStorageSync('user')
    this.setData({
      user:user
    })
    this.getAllAddress()
  },
   // ListTouch触摸开始
   ListTouchStart(e) {
    this.setData({
      ListTouchStart: e.touches[0].pageX
    })
  },

  // ListTouch计算方向
  ListTouchMove(e) {
    this.setData({
      ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
    })
  },

  // ListTouch计算滚动
  ListTouchEnd(e) {
    if (this.data.ListTouchDirection =='left'){
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
    } else {
      this.setData({
        modalName: null
      })
    }
    this.setData({
      ListTouchDirection: null
    })
  },
  getAllAddress(){
    app.request({
      url: app.globalData.apiUrl + "/address/"+this.data.user.id,
      method: 'GET',
    }).then(res => {
      if (res.statusCode == 200) {
        this.setData({
          list:res.data.data,
          loadModal:false
        })
      } else {
        this.setData({
          loadModal:false
        })
        Notify('服务错误，稍后重试');
      }
    })
  },
  remove(e){
    app.request({
      url: app.globalData.apiUrl + "/address/"+e.target.dataset.id,
      method: 'delete',
    }).then(res => {
      if (res.statusCode == 200) {
        this.getAllAddress()
      } else {
        Notify('服务错误，稍后重试');
      }
    })
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  add(){
    var address=JSON.stringify({
      tag:this.data.tag,
      address:this.data.address,
      uid:this.data.user.id
    })
    app.request({
      url: app.globalData.apiUrl + "/address/"+address,
      method: 'POST',
    }).then(res => {
      if (res.statusCode == 200) {
        this.setData({
          tag:'',
          address:'',
          modalName:''
        })
        this.getAllAddress()
      } else {
        Notify('服务错误，稍后重试');
      }
    })
  }
})